package com.offcn.sellergoods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.offcn.sellergoods.dao.SpecificationMapper;
import com.offcn.sellergoods.dao.SpecificationOptionMapper;
import com.offcn.sellergoods.entity.SpecEntity;
import com.offcn.sellergoods.pojo.Specification;
import com.offcn.sellergoods.pojo.SpecificationOption;
import com.offcn.sellergoods.service.SpecEntityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

@Transactional
@Service
public class SpecEntityServiceImpl implements SpecEntityService {

    @Autowired(required = false)
    private SpecificationMapper specMapper;

    @Autowired(required = false)
    private SpecificationOptionMapper optionMapper;

    @Override
    public void add(SpecEntity specEntity) {
        Specification spec = specEntity.getSpecification();
        specMapper.insert(spec); // 保存规格表数据
        // 将刚刚插入的规格ID 获取过来
        Long specId = spec.getId();
        // 获取规格选项
        List<SpecificationOption> specOptionList = specEntity.getSpecOptionList();
        for (SpecificationOption option : specOptionList) {
            option.setSpecId(specId);
            optionMapper.insert(option);
        }
    }

    @Override
    public SpecEntity findById(Long specId) {
        // 先通过specId查询规格
        Specification specification = specMapper.selectById(specId);
        // 再查询规格选项
        QueryWrapper<SpecificationOption> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("spec_id",specId);
        List<SpecificationOption> optionList = optionMapper.selectList(queryWrapper);
        return new SpecEntity(specification,optionList);
    }

    @Override
    public void update(SpecEntity specEntity) {
        // 1.修改规格表的数据
        specMapper.updateById(specEntity.getSpecification());
        // 2.删除规格选项
        Long specId = specEntity.getSpecification().getId();
        QueryWrapper<SpecificationOption> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("spec_id",specId);
        optionMapper.delete(queryWrapper);
        // 3.添加规格选项
        List<SpecificationOption> optionList = specEntity.getSpecOptionList();
        if (!CollectionUtils.isEmpty(optionList)) {
            for (SpecificationOption option : optionList) {
                option.setSpecId(specId);
                optionMapper.insert(option);
            }
        }
    }

    @Override
    public void delete(Long specId) {
        // 先删除规格
        specMapper.deleteById(specId);
        // 再删除关联的规格选项
        QueryWrapper<SpecificationOption> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("spec_id",specId);
        optionMapper.delete(queryWrapper);
    }

    @Override
    public List<Map> selectOptionSepc() {
        return specMapper.selectOptionSepc();
    }
}
